package sddz.foodmanage.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhy.autolayout.AutoRelativeLayout;

import java.util.List;

import sddz.foodmanage.R;
import sddz.foodmanage.activity.WaitCommentActivity;
import sddz.foodmanage.model.CommodityBean;
import sddz.foodmanage.model.WaitListBean;
import sddz.foodmanage.utils.ImageLoader;

/**
 * @Date:2019/7/11
 * @Author:lbb
 * 待审核适配器
 */

public class WaitCheckAdapter extends BaseAdapter {
    private Context context;
    private List<WaitListBean.DataBean.GoodsBean> list;
    private WaitCheckAdapter.ModifyCountInterface modifyCountInterface;
    private int userid;
    //商品id
    private int carid;
    //商品数量
    private String num;
    public WaitCheckAdapter(){

    }
    public void setdata(Context context,List<WaitListBean.DataBean.GoodsBean> list,int userid){
        this.context=context;
        this.list=list;
        this.userid=userid;
    }
    public WaitCheckAdapter(Context context,List<WaitListBean.DataBean.GoodsBean> list){
        this.context=context;
        this.list=list;
    }
    public void setModifyCountInterface(WaitCheckAdapter.ModifyCountInterface modifyCountInterface){
        this.modifyCountInterface=modifyCountInterface;
        if(this.modifyCountInterface==null){
            Log.e("modifyCountInterface","为null");
        }else{
            Log.e("modifyCountInterface","不为空");
        }
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final WaitCheckAdapter.ViewHolder holder;

        if(convertView == null){
            LayoutInflater mInflater = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.item_waitcheck, null);
            holder = new WaitCheckAdapter.ViewHolder();
            holder.tvname = convertView.findViewById(R.id.tv_name);
            holder.tvunit = convertView.findViewById(R.id.tv_unit);
            holder.tvxdweight = convertView.findViewById(R.id.tv_xdweight);
            holder.tvweight = convertView.findViewById(R.id.tv_weight);
            holder.jia = convertView.findViewById(R.id.image_add);
            holder.jian = convertView.findViewById(R.id.image_jian);
            holder.remark = convertView.findViewById(R.id.iv_remark);
            convertView.setTag(holder);
        }else{
            holder = (WaitCheckAdapter.ViewHolder) convertView.getTag();
        }


        final WaitListBean.DataBean.GoodsBean info = list.get(position);
        if(info != null){

            holder.tvname.setText(list.get(position).getName());
            holder.tvunit.setText(list.get(position).getPrice() + "元/" + list.get(position).getUnit());
            holder.tvxdweight.setText(list.get(position).getAmount_data()+list.get(position).getUnit());//下单数量
            holder.tvweight.setText(list.get(position).getAmount()+"");//真实数量

            holder.jia.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    modifyCountInterface.doIncrease( holder.tvweight,position);// 暴露增加接口
                }
            });
            holder.jian.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    modifyCountInterface.doDecrease( holder.tvweight,position);// 暴露删减接口
                }
            });
            holder.tvweight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    modifyCountInterface.doTextView(holder.tvweight,position);
                }
            });
            //======================================================================================
            final String remark = list.get(position).getRemark();
            if(remark.isEmpty()){
                holder.remark.setImageResource(R.mipmap.icon_remark_gray);
            }else{
                holder.remark.setImageResource(R.mipmap.icon_remark_red);
            }
            holder.remark.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(!remark.isEmpty()){
                        final EditText editText = new EditText(context);
                        editText.setFocusable(false);
                        editText.setText(remark);
                        AlertDialog.Builder inputDialog =
                                new AlertDialog.Builder(context);
                        inputDialog.setTitle("备注信息:")
                                .setView(editText)
                                .setPositiveButton("关闭", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {

                                    }
                                })
                                .show();;

                    }
                }
            });
            //======================================================================================
        }

        return convertView;
    }




    class ViewHolder{
        ImageView jia,jian;
        TextView tvname,tvunit,tvxdweight;
        TextView tvweight;
        ImageView remark;
    }
    /**
     * 改变数量的接口
     */
    public interface ModifyCountInterface {
        /**
         * 增加操作
         *
         *
         * @param showCountView 用于展示变化后数量的View
         */
        public void doIncrease(View showCountView,int position);

        /**
         * 删减操作
         *
         *
         * @param showCountView 用于展示变化后数量的View
         */
        public void doDecrease(View showCountView,int position);
        public void doTextView(View showCountView,int position);

    }
}
